/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: StaffConverter.java 46 2012-09-19 09:10:23Z zhuxiaopeng $
 * $Revision: 46 $
 * $Date: 2012-09-19 17:10:23 +0800 (三, 2012-09-19) $
 * $Author: zhuxiaopeng $
 */
package daily.web;

import javax.faces.component.*;
import javax.faces.context.*;
import javax.faces.convert.*;
import javax.inject.*;

import daily.entity.*;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 46 $ - $Date: 2012-09-19 17:10:23 +0800 (三, 2012-09-19) $
 */
@Named
public class StaffConverter implements Converter {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.String)
	 */
	@Override
	public Object getAsObject(final FacesContext context, final UIComponent component, final String value) {
		for (final Staff staff : sessionBean.getStaffList()) {
			if (staff.getEntityId().toString().equals(value)) {
				return staff;
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public String getAsString(final FacesContext context, final UIComponent component, final Object value) {
		if (value == null) {
			return null;
		}
		final Staff staff = (Staff) value;
		return staff.getEntityId().toString();
	}

	@Inject
	private SessionBean sessionBean;
}
